Repository navigation
feat: feature refresh listeners on both clients - #142
vazarkevych wants to merge 7 commits into
Conversation
Confidence Score: 5/5
[Medium impact] The pull request appears safe to merge, with no outstanding correctness, security, or repository-rule issues identified. Reviews (9) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile |
05c7bd9 to
604849b
Compare
|
All three are valid — fixed in the force-push. Failed Refresh Replays Payload. Confirmed: Concurrent Refreshes Reorder Events. Also confirmed — and worth noting the Close Retains Refresh Listeners. Fixed; Three regression tests, each failing without its fix. The ordering one runs six |
|
Maintainer decision needed: confirm the refresh-listener contract before implementation review. Listeners receive the raw payload after it is applied, and async listeners are awaited in order, so a slow listener delays later refresh notifications. The async remote-evaluation path does not emit these events, while the synchronous client can emit them for user-specific payloads. Please record the intended ordering, callback/re-entry expectations, and remote-evaluation coverage, coordinating the shared-client changes with #114 and #141. |
|
Recording the contract. One of your four points turned out to be broken rather than just undocumented, so that's fixed in the force-push.
On coordination: #114 touches |
Applications had no supported way to react to a feature payload being applied. The plumbing existed but wasn't reachable: the sync path exposed it only on the module-level feature_repo singleton, and the async path only through client._features_repository, a private attribute, with an async-only callback. Adds add_feature_refresh_listener / remove_feature_refresh_listener to both GrowthBook and GrowthBookClient. The add method returns a callable that unregisters the listener. Listeners receive the raw payload dict and run after it has been applied, so evaluating from inside a listener already sees the new definitions. A listener that raises is logged and skipped. The async client accepts plain functions and coroutine functions alike. Also fixes a gap on the way: the sync streaming path applied payloads and wrote the cache without notifying anything, so in SSE mode — the case this feature exists for — nothing observed the update. Three ordering and lifecycle details, each with a regression test: Dispatch is keyed on payload identity, so listeners fire once per refresh rather than once per evaluation (evaluation calls load_features() constantly and usually gets a cache hit, which returns the same object). A failed refresh no longer clears that identity marker. It applies nothing, so the payload listeners last saw is unchanged; clearing it made the next cache hit — which returns that same previously-notified object — look fresh and fire listeners a second time. Apply and notify happen as one unit. In the sync client that is the reentrant payload-writer lock, which evaluation never takes. In the async client it is a separate ordering lock, since evaluation takes the context lock and notifying under that one deadlocks any listener that evaluates. Without this, two overlapping refreshes could apply A, apply and notify B, then notify A — handing listeners an older payload last and letting a listener see state that doesn't match the payload it was given. GrowthBookClient.close() clears the listener list, matching subscriptions and the sync client. Listener closures capture application objects, and a reinitialized client would otherwise fire callbacks from its previous lifecycle.
604849b to
6d8aef0
Compare
|
@greptile review |
A listener that updated features re-entered the dispatch: - Async client: _feature_update_callback waited for the ordering lock that its own dispatch held, so a listener calling set_payload, set_features or initialize hung the client. - Sync client: the reentrant payload lock let the nested refresh deliver the newer payload to every listener before the outer loop resumed, so the remaining listeners got the older payload last, while evaluation already used the newer one. A refresh started from a listener now only applies its payload. The running dispatch delivers it: the remaining listeners get only the newer payload, and delivery starts over with it. The sync client recognises re-entry by a flag held under its payload lock. The async client uses a context variable carrying the running dispatch's token, so a task the listener awaits counts as re-entry too, while a concurrent refresh still waits for the lock. Regression tests cover both clients; each fails without the fix (the async ones by timeout).
|
@greptile review |
A task that a listener starts without awaiting inherits the dispatch context, so its set_payload() takes the re-entry path and skips the ordering lock. If the dispatch finished while the task was still applying, the payload was applied but never delivered to listeners. After a re-entrant apply, if no dispatch is running any more, the task now takes the ordering lock and dispatches the payload itself, unless a newer payload has replaced it or another dispatch already delivered it.
|
@greptile review |
1 similar comment
|
@greptile review |
…listeners # Conflicts: # growthbook/growthbook_client.py
|
@greptile review |
_notify_feature_refresh skipped a payload only when it was the same object as the last one dispatched. A cache that decodes on every hit, like the README's Redis cache, returns a new dict each time, so every load_features() fired the listeners again, and a listener that loads features itself kept the delivery loop from ever finishing. Also skip a payload equal to the last one. The SSE handler applied and dispatched the streamed payload before caching it. A listener calling load_features() then got the previous payload from the cache and reapplied it over the update. Cache the payload first.
|
@greptile review |
…ache errors The content check that skips repeated payloads used ==, which treats True == 1, False == 0 and 1 == 1.0 as equal. A setting changing between those values was applied but never reached the refresh listeners. Compare payloads structurally with types kept apart instead. The SSE handler now caches before applying, so a failing custom cache (e.g. a Redis outage) raised before the update was applied, and the stream does not retry the event. Log the failed write and apply the payload anyway.
|
@greptile review |
…listeners # Conflicts: # tests/test_growthbook.py
|
@greptile review |
What and why
Applications had no supported way to react to a feature payload being applied. The sync client exposed it only on the module-level
feature_reposingleton, the async client only through the privateclient._features_repository.What changed
New API on
GrowthBookandGrowthBookClient:add_feature_refresh_listener(listener)returns an unsubscribe callable;remove_feature_refresh_listener(listener)also works. The async client accepts plain and coroutine functions.Semantics: listeners get the raw payload after it is applied, so evaluating inside a listener sees the new definitions. A raising listener is logged and skipped. A failed refresh notifies nothing.
Once per refresh (sync): a payload is skipped if it is the same object or has the same content as the last one delivered. Content comparison keeps JSON types apart (
True≠1≠1.0), so caches that return a copy each time, like the README's Redis cache, don't refire listeners.Ordering and re-entry: apply and notify run as one unit, so overlapping refreshes arrive in apply order. A listener may refresh features itself (directly or from a task): the remaining listeners get only the newer payload, and nothing older than the applied state is delivered. The async client notifies outside the context lock, so there is no deadlock.
SSE (sync): streamed payloads now notify listeners. They are cached before listeners run. A failed cache write is logged and the update is still applied.
Cleanup:
close()/destroy()clear listeners.Remote-eval: the async client emits no events (payloads there are per-user); possible follow-up on top of feat: user-scoped GrowthBookClient instances via create_scoped_instance #141.
README: new "Feature Refresh Listeners" section.
How to test
pytest tests/test_growthbook.py tests/test_growthbook_client.py -k "refresh or sse_payload"17 new tests (21 cases) covering both clients: once per refresh, copying caches, bool/number changes, SSE delivery and cache-write failures, unsubscribe and errors,
close(), concurrent refreshes, and re-entrant refreshes without deadlock or reordering.Full suite: 1962 passed. The 2
cachefixture errors intest_growthbook_client.pyalso happen onmain.Size
Production code +276 / −6, tests +546, README +43. Over 800 lines, mostly tests.
Migration
None. Nothing changes unless a listener is registered.